﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace FRL.DebtChased.Silverlight.Library
{
    public partial class AgentRatesCtrl : UserControl, INotifyPropertyChanged
    {
        public AgentRatesCtrl()
        {
            InitializeComponent();
            this.LayoutRoot.DataContext = this;
            List<AgentRatesDummy> blank = new List<AgentRatesDummy>();
            this.ActiveCollection = blank;
        }

        /////////////////////////////////////////////////////////////
        // Interface & Constructor

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        /////////////////////////////////////////////////////////////
        // Control "Parameter"

        private List<AgentRatesDummy> _activeCollection;
        public List<AgentRatesDummy> ActiveCollection
        {
            get { return _activeCollection; }
            set
            {
                if (ActiveRecordEdited)
                    SaveRecordChanges();

                _activeCollection = value;
                RaisePropertyChanged("ActiveCollection");

                if (_activeCollection == null)
                    this.ActiveRecord = null;
                else if (_activeCollection.Count > 0)
                    this.ActiveRecord = _activeCollection[0];
                else
                    this.ActiveRecord = NewRecord;
            }
        }

        /////////////////////////////////////////////////////////////
        // Properties

        private AgentRatesDummy _newRecord;
        private AgentRatesDummy NewRecord
        {
            get
            {
                if (_newRecord == null)
                {
                    _newRecord = new AgentRatesDummy();
                }
                return _newRecord;
            }
            set { _newRecord = value; }
        }

        private AgentRatesDummy _activeRecord;
        public AgentRatesDummy ActiveRecord
        {
            get { return _activeRecord; }
            set
            {
                if ((object.ReferenceEquals(this._activeRecord, value) != true))
                {
                    if (ActiveRecordEdited) SaveRecordChanges();

                    _activeRecord = value;
                    ActiveRecordEdited = false;

                    RaisePropertyChanged("ActiveRecord");
                    RaisePropertyChanged("RecordTabs");

                    InEditMode = (_activeRecord == NewRecord) ? true : EditMode;
                }
            }
        }

        public IEnumerable<TabItem> RecordTabs
        {
            get
            {
                List<TabItem> tabs = new List<TabItem>();
                if (ActiveCollection != null)
                {
                        foreach (AgentRatesDummy record in ActiveCollection)
                        {

                            TabItem tab = new TabItem() { Header = string.Format("{0}", record.Coverage), Tag = record };
                            tabs.Add(tab);
                        }
                    tabs.Add(new TabItem() { Header = "*New*", Tag = NewRecord });
                }
                return tabs;
            }
        }

        private bool _activeRecordEdited;
        public bool ActiveRecordEdited
        {
            get { return _activeRecordEdited; }
            set
            {
                if (value != _activeRecordEdited)
                {
                    _activeRecordEdited = value;
                    RaisePropertyChanged("ActiveRecordEdited");
                }
            }
        }

        private bool _EditMode;
        public bool EditMode
        {
            get { return _EditMode; }
            set
            {
                _EditMode = value;
                InEditMode = ActiveRecord != null ? _EditMode : false;
            }
        }
        private bool _inEditMode;
        private bool InEditMode
        {
            get { return _inEditMode; }
            set
            {
                if (value != _inEditMode)
                {
                    _inEditMode = value;
                    if (value)
                    {
                     }
                    else
                    {
                    }
                    RaisePropertyChanged("InEditMode");
                }
            }
        }

        /////////////////////////////////////////////////////////////
        // Methods

        private bool SavingRecordChanges { get; set; }
        private void SaveRecordChanges()
        {
            SavingRecordChanges = true;
            if (ActiveCollection != null)
            {
                if (_activeRecord == NewRecord)
                {
                    ActiveCollection.Add(NewRecord);
                    //MessageBox.Show(string.Format("Save new address for \n{0}", ActiveAddressSummary));
                    NewRecord = null;
                }
                else
                {
                    MessageBox.Show(string.Format("Save record changes"));
                }
            }
            ActiveRecordEdited = false;

            RaisePropertyChanged("RecordTabs");
            SavingRecordChanges = false;
        }

        /////////////////////////////////////////////////////////////
        // Event Handlers

        private void AddressEditModeToggleButton_Click(object sender, EventArgs e)
        {
            EditMode = !EditMode;
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabControl ctrl = (TabControl)sender;
            if (ctrl.SelectedIndex >= 0)
            {
                TabItem tab = (TabItem)ctrl.Items[ctrl.SelectedIndex];
                ActiveRecord = (AgentRatesDummy)tab.Tag;
            }
        }
        private void RecordEditGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (RefData.KeyIsEditKey(e.Key))
            {
                if (_activeRecord == NewRecord)
                {
                    int selectedIndex = AddressTabControl.SelectedIndex;
                    SaveRecordChanges();
                    InEditMode = true;
                    AddressTabControl.SelectedIndex = selectedIndex;
                }
                else
                {
                    ActiveRecordEdited = true;
                }
            }
        }


    }

    public class AgentRatesDummy
    {
        public int Id { get; set; }
        public string Coverage { get; set; }
        public string RateType { get; set; }
        public List<AgentRateDummy> Rates { get; set; }

        public AgentRatesDummy()
        {
        }
    }

    public class AgentRateDummy
    {
        public int Id {get;set;}
        public int From { get; set; }
        public int To { get; set; }
        public double rate { get; set; }

        public AgentRateDummy()
        {
        }
    }

}
